<?php
/* SVN FILE: $Id: named_controller.php 107 2007-04-14 17:07:09Z Andy $ */

/**
 * Short description for file.php.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Copyright (c), Andy Dawson
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright    Copyright (c) 2007, Andy Dawson
 * @package      noswad
 * @subpackage   noswad.app.plugins.pagination.controllers
 * @since        Noswad site version 3
 * @todo         TODO Edit this block
 * @version      $Revision: 107 $
 * @created      26/01/2007
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2007-04-14 19:07:09 +0200 (sáb, 14 abr 2007) $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Short description for Class
 *
 * Long descrption of Class
 */

/**
 * Description for var
 *
 * @var type
 * @access public/private/protected
 */

/**
 * Description for method
 *
 * @param type $name description
 * @return type description
 */
 class NamedController extends PaginationAppController {
	var $name = 'Named'; // required for PHP4 installs

	function beforeFilter () {
		$this->Pagination->ajaxAutoDetect = false; // overiding
		$this->Pagination->paramStyle = "pretty"; // overiding
		$this->Pagination->paramSeperator = ":"; // overiding
		$this->viewPath = 'normal'; // Use the generic views
		parent::beforeFilter();
	}

	function index() {
		$params = $this->_namedParse();
		list($order,$limit,$page) = $this->Pagination->init($criteria=null,$params);
		$this->data = $this->{$this->modelClass}->findAll($criteria=null, null, $order, $limit, $page);
		$this->render('index');
	}

	function old_style_index() {
		$criteria = null;
		$params = $this->_namedParse();
		$this->Pagination->init($criteria,$params);
		$this->data = $this->{$this->modelClass}->findAll($criteria, null, $this->Pagination->order, $this->Pagination->limit, $this->Pagination->page);
		$this->render('index');
	}

	function custom_index() {
		$options = array (
						"total"=>200, // overriding the number of results
						"sortByClass"=>"Category", // Different default sort class
						"sortBy"=>"name", // Different default sort field
						"showLimits"=>false // override the default to not show limits
						);
		$params = $this->_namedParse();
		list($order,$limit,$page) = $this->Pagination->init($criteria=null,$params,$options);
		$this->data = $this->{$this->modelClass}->findAll($criteria=null, null, $order, $limit, $page);
		$this->render('custom_index');
	}

	function extra_get_params() {
		if (count($_GET)<3) {
			return $this->redirect(array('?' => 'this=has&nothing=todo&with=pagination'));
		}
		$criteria = null;
		$params = $this->_namedParse();
		list($order,$limit,$page) = $this->Pagination->init($criteria=null,$params);
		$this->data = $this->{$this->modelClass}->findAll($criteria, null, $order, $limit, $page);
		$this->render('index');
	}

	function extra_params() {
		if (!$this->params['pass']) {
			return $this->redirect(array('this','has','nothing','todo','with','pagination'));
		}
		$criteria = null;
		$params = $this->_namedParse();
		list($order,$limit,$page) = $this->Pagination->init($criteria=null,$params);
		$this->data = $this->{$this->modelClass}->findAll($criteria, null, $order, $limit, $page);
		$this->render('index');
	}

	function no_limits() {
		$criteria = null;
		$options = array (
						"showLimits"=>false // override the default to not show limits
						);
		$params = $this->_namedParse();
		list($order,$limit,$page) = $this->Pagination->init($criteria=null,$params,$options);
		$this->data = $this->{$this->modelClass}->findAll($criteria, null, $order, $limit, $page);
		$this->render('index');
	}

	function _namedParse () {
		$args = $this->passed_args;
		$c = count($args);
		if (is_array($args)) {
			foreach ($args as $arg) {
	            if(strpos($arg, ':')) {
	                list($name, $value) = split(':', $arg);
					$a[$name] = $value;
	            } else {
					$a['_unamedParameters'][] = $arg;
				}
			}
		return $a;
		} else {
			return array();
		}
	}

    function _param_convert($paramlist, $label, $default = null) {
        foreach($paramlist as $param) {
            if(strpos($param, ':')) {
                list($name, $value) = split(':', $param);
                if($name == $label) {
                    // if we found the value then return it
                    return $value;
                }
            }
        }
        return $default;
    }
}
?>